package jannex.label;

import java.util.Arrays;

class ArrayLabelList extends LabelList {

    private final String[] labels;

    ArrayLabelList(String[] labels) {
        this.labels = labels;
    }

    @Override
    public int size() {
        return labels.length;
    }

    @Override
    public String get(int index) {
        return labels[index];
    }

    public String[] toArray() {
        return labels.clone();
    }

    @SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
    public <T> T[] toArray(T[] a) {
        int size = size();
        if (a.length < size) {
            return Arrays.copyOf(this.labels, size, (Class<? extends T[]>) a.getClass());
        }
        System.arraycopy(this.labels, 0, a, 0, size);
        if (a.length > size) {
            a[size] = null;
        }
        return a;
    }

    public int indexOf(Object o) {
        if (o == null || !(o instanceof String)) {
            return -1;
        }
        String l = o.toString();
        for (int i = 0; i < labels.length; i++) {
            if (l.equalsIgnoreCase(labels[i])) {
                return i;
            }
        }
        return -1;
    }

    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }
}
